﻿/***************************
* 1.新闻列表
* 2.新闻详情
****************************/

/* 变量 */
var preferential = "booking/Preferential.html";

/* 新闻 */
var contactsCallback_preferential = myApp.onPageInit('preferential', function (page) {

    // 过滤
    $$(".navbar-inner .right img").hide();

    //删除滚动后重新添加滚动
    var flag_InfiniteScroll = true;
    //是否需要滚动加载
    var flag_needScroll = true;
    //滚动事件只执行一次
    var flag_execute = false;

    var index_tab1 = 1;
    var index_tab2 = 1;
    var index_tab3 = 1;
    var index_tab4 = 1;
    var current_tabindex = 0;
    var size = 10;

    LoadNewsAll(index_tab1, size);
    //加载数据
    function LoadNewsAll(pageindex, pagesize) {

        // 检索条件
        var v_search = [];
        var v_order = [];

        var Identifier = "";
        if (current_tabindex == 0) {
            Identifier = "mobile/sale/attraction";
        }
        else if (current_tabindex == 1) {
            Identifier = "mobile/sale/hotel";
        }
        else if (current_tabindex == 2) {
            Identifier = "mobile/sale/line";
        }
        


       

        var conditionParams = JSON.stringify({
            type: 7,
            Identifier: Identifier,
            pageIndex: pageindex,
            pageSize: pagesize,
        });

        $$.ajax({
            url: ISS_Constants.Url.defaulturl + ISS_Constants.serviceName.CMS,
            crossDomain: true,
            data: { content: conditionParams },
            dataType: "jsonp",
            method: "POST",
            success: function (data, status) {
                data = JSON.parse(data);
                if (data && data.results.length > 0) {
                    for (var i = 0; i < data.results.length; i++) {
                        for(var j = 0;j<data.results[i].value.length;j++){
                            var timer = data.results[i].value[j].CreateDate.split(" ");
                            if (timer.length > 0) {
                                data.results[i].value[j].CreateDate = timer[0];
                            }
                            if (data.results[i].value[j].F_LargeImage == undefined ||
                                data.results[i].value[j].F_LargeImage == null || data.results[i].F_LargeImage == "") {
                                data.results[i].value[j].F_LargeImage = NoPicture();
                            };
                            if (current_tabindex == 0){
                                data.results[i].value[j].Url = "ticketDetail.html?ticketdetail_guid="+data.results[i].value[j].WEB_BASE_ATTRACTIONS_Code;
                            }else if(current_tabindex == 1){
                                data.results[i].value[j].Url = "hotelDetail.html?code="+data.results[i].value[j].WEB_BASE_HOTEL_Code;
                            }else if(current_tabindex == 2){
                                data.results[i].value[j].Url = "lineDetail.html?ID="+data.results[i].value[j].WEB_B2C_Lines_ID+"&linedetail_guid="+data.results[i].value[j].WEB_B2C_Lines_Code;
                                
                            }
                        }
                       
                    };

                    var searchTemplate = $$('#template-preferential-tab1').html();
                    if (searchTemplate) {
                        var compiledTemplate = Template7.compile(searchTemplate);
                        var html = compiledTemplate(data.results[0]);
                        if (current_tabindex == 0) $$("#preferential_page #sub_preferential_tab1 .content-block ul").append(html);
                        else if (current_tabindex == 1) $$("#preferential_page #sub_preferential_tab2 .content-block ul").append(html);
                        else if (current_tabindex == 2) $$("#preferential_page #sub_preferential_tab3 .content-block ul").append(html);
                        flag_InfiniteScroll = flag_needScroll = true;
                    }

                    //tab1综合,无限滚动只加载一次
                    if (!flag_execute) {
                        //只执行一次
                        flag_execute = true;

                        // 加载flag
                        var loading = false;
                        $$('.infinite-scroll').on('infinite', function () {
                            //综合 价格 距离 切换
                            if (!flag_needScroll) {
                                flag_needScroll = true;
                                return;
                            }

                            //查询时,删除滚动后重新绑定pageindex
                            if (!flag_InfiniteScroll) {
                                flag_InfiniteScroll = true;
                                return;
                            }

                            if (true) { // 只有一个TAB,所以不需要
                                // 如果正在加载，则退出
                                if (loading) return;
                                // 设置flag
                                loading = true;
                                // 模拟1s的加载过程
                                setTimeout(function () {
                                    // 重置加载flag
                                    loading = false;
                                    if (current_tabindex == 0) {
                                        index_tab1++;
                                        LoadNewsAll(index_tab1, pagesize);
                                    }
                                    else if (current_tabindex == 1) {
                                        index_tab2++;
                                        LoadNewsAll(index_tab2, pagesize);
                                    }
                                     else if (current_tabindex == 2) {
                                        index_tab3++;
                                        LoadNewsAll(index_tab3, pagesize);
                                    }
                                }, 100);
                            }
                        });
                    }
                    if (current_tabindex == 0 && $$("#sub_preferential_tab1 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader1').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 1 && $$("#sub_preferential_tab2 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader2').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 2 && $$("#sub_preferential_tab3 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader3').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }
                    else if (current_tabindex == 3 && $$("#sub_preferential_tab4 .content-block ul li").length >= data.total) {
                        // 删除加载提示符
                        $$('.infinite-scroll-preloader3').remove();
                        // 加载完毕，则注销无限加载事件，以防不必要的加载
                        myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    }

                }
                else {
                    // 删除加载提示符
                    myApp.detachInfiniteScroll($$('.infinite-scroll'));
                    if (current_tabindex == 0) {
                        $$('.infinite-scroll-preloader1').remove();
                        $$("#preferential_page #sub_preferential_tab1 .content-block .noAnswer").remove();
                        if (pageindex == 1)
                            $$("#preferential_page #sub_preferential_tab1 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                    else if (current_tabindex == 1) {
                        $$('.infinite-scroll-preloader2').remove();
                        $$("#preferential_page #sub_preferential_tab2 .content-block .noAnswer").remove();
                        if (pageindex == 1)
                            $$("#preferential_page #sub_preferential_tab2 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                     else if (current_tabindex == 2) {
                        $$('.infinite-scroll-preloader3').remove();
                        $$("#preferential_page #sub_preferential_tab3 .content-block .noAnswer").remove();
                        if (pageindex == 1)
                            $$("#preferential_page #sub_preferential_tab3 .content-block").append("<div id='noAnswer' style='margin:auto;text-align:center'>对不起，您搜索的结果不存在！</div>");
                    }
                }
            },
            error: function (xhr, status) { }
        });
    }

    //切换
    $$("#preferential_page #sub_buttons_row a").on("click", function () {
        $$('.infinite-scroll').scrollTop(0);
        current_tabindex = $$(this).index();
        myApp.attachInfiniteScroll($$('.infinite-scroll'));

        flag_InfiniteScroll = true;
        flag_needScroll = false;
        if ($$(this).index() == 0 && $$("#preferential_page #sub_preferential_tab1 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab1, size);
        }
        else if ($$(this).index() == 1 && $$("#preferential_page #sub_preferential_tab2 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab2, size);
        }
        else if ($$(this).index() == 2 && $$("#preferential_page #sub_preferential_tab3 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab3, size);
        }
        else if ($$(this).index() == 3 && $$("#preferential_page #sub_preferential_tab4 .content-block ul").children("li").length == 0) {
            LoadNewsAll(index_tab4, size);
        }
    });
});


/* 初始化  */
function initData() {
    var href = window.location.href;
    if (href.indexOf(preferential) > -1) {
        contactsCallback_preferential.trigger();
    }
   
}

initData();

